/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.Address;
import com.kaaction.modules.mer.dao.AddressDao;

/**
 * 地址信息Service
 * @author huanghaiming
 * @version 2017-05-17
 */
@Service
@Transactional(readOnly = true)
public class AddressService extends CrudService<AddressDao, Address> {

	public Address get(String id) {
		return super.get(id);
	}
	
	public List<Address> findList(Address address) {
		return super.findList(address);
	}
	
	public Page<Address> findPage(Page<Address> page, Address address) {
		return super.findPage(page, address);
	}
	
	@Transactional(readOnly = false)
	public void save(Address address) {
		super.save(address);
	}
	
	@Transactional(readOnly = false)
	public void delete(Address address) {
		super.delete(address);
	}

	@Transactional(readOnly = false)
	public void updOrInsAddress(Address address) {
		if(StringUtils.isBlank(address.getId())){
			
			int cnt = super.dao.selectCnt(address.getThirdpartyId());
			if(cnt==0){
				address.preInsert();
				dao.insert(address);
			}else{
				if("1".equals(address.getIsDefault())){
					super.dao.updateIsDefault(address.getThirdpartyId());
				}
				address.preInsert();
				dao.insert(address);
			}
		}else{
			if("1".equals(address.getIsDefault())){
				super.dao.updateIsDefault(address.getThirdpartyId());
			}
			super.dao.updateAddress(address);
		}
	}

	public List<Map<String, Object>> selAddress(Address address) {
		return super.dao.selAddress(address);
	}

	@Transactional(readOnly = false)
	public void updateAddr(Address address) {
		super.dao.updateAddr(address.getThirdpartyId(),"");
	}

	@Transactional(readOnly = false)
	public void cancelAddress(Address address) {
		address.setIsDefault("0");
		super.dao.updIsDef(address);
		super.dao.updateAddr(address.getThirdpartyId(),address.getId());
		
	}
	
}